﻿using System;
using Microsoft.Xrm.Sdk.Client;
using System.ServiceModel.Description;
using System.Configuration;
using Microsoft.Xrm.Sdk;

namespace Founder.PKURG.CRMCommon
{
    public class AdminCrmConnect
    {
        private static OrganizationServiceProxy _OrgService;
       
        public static IOrganizationService OrgService
        {
            get
            {
                if (true)
                {
                   // IOrganizationService orgService = null;
                    string isIFD = CommonConstants.SERVICE_FOUNDERCRM_ISIFD.ToLower().Trim();
                    if (isIFD == Boolean.TrueString.ToString().ToLower())
                    {
                        string orgServiceUriString = CommonConstants.SERVICE_FOUNDERCRM_IFDORGSERVICEURI;

                        string userName = CommonConstants.SERVICE_FOUNDERCRM_IFDUSERNAME;
                        string password = CommonConstants.SERVICE_FOUNDERCRM_IFDUSERPASSWORD;
                        string domainName = CommonConstants.SERVICE_FOUNDERCRM_IFDDOMAINNAME;

                        Uri orgServiceUri = new Uri(orgServiceUriString);
                        ClientCredentials credentials = new ClientCredentials();
                        credentials.UserName.UserName = domainName + "\\" + userName;
                        credentials.UserName.Password = password;

                        _OrgService = new OrganizationServiceProxy(orgServiceUri, null, credentials, null);
                    }
                    else if (isIFD == Boolean.FalseString.ToString().ToLower())
                    {
                        string orgServiceUriString = CommonConstants.SERVICE_FOUNDERCRM_CRMORGSERVICEURI;

                        string userName = CommonConstants.SERVICE_FOUNDERCRM_CRMUSERNAME;
                        string password = CommonConstants.SERVICE_FOUNDERCRM_CRMUSERPASSWORD;
                        string domainName = CommonConstants.SERVICE_FOUNDERCRM_CRMUSERDOMINNAME;
                        Uri orgServiceUri = new Uri(orgServiceUriString);
                        ClientCredentials credentials = new ClientCredentials();
                        credentials.Windows.ClientCredential = new System.Net.NetworkCredential(userName, password, domainName);                      

                        _OrgService = new OrganizationServiceProxy(orgServiceUri, null, credentials, null);

                        

                    }
                }
                
                return _OrgService;
            }
        }

        private static OrganizationServiceContext _orgServiceContext;
        public static OrganizationServiceContext orgServiceContext
        {
            get
            {
                if (_orgServiceContext == null)
                    _orgServiceContext = new OrganizationServiceContext(OrgService);
                return _orgServiceContext;
            }
        }

        public void init()
        {
            _OrgService = null;
            _orgServiceContext = null;
        }
    }
}